# import asyncio
# import threading
#
# class AsyncThread(threading.Thread):
#     def __init__(self, *args, **kwargs):
#         super().__init__()
#         self.args = args
#         self.kwargs = kwargs
#         self.loop = None
#
#     def run(self):
#         self.loop = asyncio.new_event_loop()
#         asyncio.set_event_loop(self.loop)
#         self.loop.run_until_complete(self.my_async_function("", ""))
#
#     def stop(self):
#         if self.loop:
#             self.loop.call_soon_threadsafe(self.loop.stop)
#
#     async def my_async_function(self, arg1, arg2):
#         loop = asyncio.get_event_loop()
#         print("Loop in async function:", id(loop))
#         # 在这里编写你的异步逻辑
#
# async def main():
#     thread = AsyncThread("Hello", "World")
#     thread.start()
#
#     loop = asyncio.get_event_loop()
#     print("Main Loop in async function:", id(loop))
#
# if __name__ == '__main__':
#     asyncio.run(main())



import asyncio
import threading

async def async_task():
    # 异步操作
    await asyncio.sleep(2)
    print(f"-----sub---thread:{id(asyncio.get_event_loop())}")
    print("异步操作完成")

def thread_function():
    # 创建独立的事件循环
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    # 运行协程函数
    loop.run_until_complete(async_task())

# 在子线程中运行事件循环
print(f"-----main---thread:{id(asyncio.get_event_loop())}")
thread = threading.Thread(target=thread_function)
thread.start()



